package com.example.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
//import static org.hamcrest.Matchers.*;

import java.io.FileReader;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Logger;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;

//import org.testng.annotations.BeforeTest;

import com.example.fw.AppManager;

public class TestBase {
	
	protected Logger log = Logger.getLogger("TEST"); 
	protected AppManager app;
	
	@BeforeClass
	@Parameters({"configFile"})
	public void setUp(@Optional String configFile) throws Exception {
		if (configFile == null) {
			configFile= System.getProperty("configFile");
		}
		if (configFile == null) {
			configFile= System.getenv("configFile");
		}
		if (configFile == null) {
			configFile= "application.properties";
		}
		Properties props = new Properties();
		props.load(new FileReader(configFile));
		log.info ("setUp start");
		app = AppManager.getInstance();
		app.setProperties(props);
		log.info ("setUp end");
	}
	
	@AfterTest
	public void tearDown() throws Exception {
		log.info ("tearDown start");
		AppManager.getInstance().stop();
		log.info ("tearDown end");
	}
	

	
//------------------groups-------------------	
	protected void verifyGroupAdded(Set<GroupObject> oldList,
			GroupObject validGroup, Set<GroupObject> newList) {
		assertThat(newList.size(), equalTo(oldList.size()+1));
		validGroup.setId("$$$");
		oldList.add(validGroup);
		validGroup.setId(null);
		assertThat(newList, equalTo(oldList));				
	}
	protected void verifyGroupRemoved(Set<GroupObject> oldList,
		GroupObject group, Set<GroupObject> newList) {

		assertThat(newList.size(), equalTo(oldList.size()-1));
		oldList.remove(group);
		assertThat(newList, equalTo(oldList));
	}

	protected void verifyGroupModificated(Set<GroupObject> oldList, GroupObject group,GroupObject group2,
			Set<GroupObject> newList) {
		
		assertThat(newList.size(), equalTo(oldList.size()));
		oldList.remove(group);
		newList.remove(group2);
		assertThat(newList, equalTo(oldList));
				
			}
//-------------------contacts-------------------
	protected void verifyContactAdded(Set<ContactObject> oldList,
			ContactObject validContact, Set<ContactObject> newList) {
	
		assertThat(newList.size(), equalTo(oldList.size()+1));
		validContact.setId("$$$");
		oldList.add(validContact);
		validContact.setId(null);
		assertThat(newList, equalTo(oldList));				
	}

	protected void verifyContactRemoved(Set<ContactObject> oldList, ContactObject contact1, Set<ContactObject> newList) {
		
		assertThat(newList.size(), equalTo(oldList.size()-1));
		oldList.remove(contact1);
		assertThat(newList, equalTo(oldList));				
	}

	protected void verifyContactModificated(Set<ContactObject> oldList, ContactObject anyContact, ContactObject newContact,
			Set<ContactObject> newList) {
		
		assertThat(newList.size(), equalTo(oldList.size()));
		oldList.remove(anyContact);
		newList.remove(newContact);
		assertThat(newList, equalTo(oldList));				
	}	
	
}
